package com.qk.controller;

import com.qk.Response.PageResult;
import com.qk.Response.Result;
import com.qk.domain.user.UserDO;
import com.qk.dto.UserDTO;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;
    /**
     * 查询用户列表(分页)
     * @return
     */
    @GetMapping
    public Result getUsers(UserDTO userDto) {
        log.info("查询用户列表：{}",userDto);
        PageResult<UserDO> pageResult =userService.getUsers(userDto);
        return Result.success(pageResult);
    }
    /**
     * 添加用户
     * @param userDO
     * @return
     */
    @PostMapping
    public Result addUser(@RequestBody UserDO userDO){
        log.info("添加用户：{}",userDO);
        userService.addUser(userDO);
        return Result.success();
    }
    /*
    批量删除用户
     */
    @DeleteMapping("/{ids}")
    public Result deleteUsers(@PathVariable List<Integer> ids){
        log.info("批量删除用户：{}",ids);
        userService.deleteUsers(ids);
        return Result.success();
    }

    /*
    修改用户
     */
    /*
    根据id查询用户
     */
    @GetMapping("/{id}")
    public Result getUserById(@PathVariable Integer id){
        log.info("查询用户：{}",id);
        UserDO userDO = userService.getUserById(id);
        return Result.success(userDO);
    }
    /*
    修改用户
     */
    @PutMapping
    public Result updateUser(@RequestBody UserDO userDO){
        log.info("修改用户：{}",userDO);
        userService.updateUser(userDO);
        return Result.success();
    }
    /*
    查询所有用户
     */
    @GetMapping("/list")
    public Result getAllUsers(){
        log.info("查询所有用户");
        return Result.success(userService.getAllUsers());
    }
    /*
    根据角色标识查询用户
     */
    @GetMapping("/role/{roleable}")
    public Result getUsersByRole(@PathVariable String lable){
        log.info("查询用户：{}",lable);
        return Result.success(userService.getUsersByRole(lable));
    }
    /*
    根据部门id查询用户
     */
    @GetMapping("/dept/{deptId}")
    public Result getUsersByDeptId(@PathVariable Integer deptId){
        log.info("查询用户：{}",deptId);
        return Result.success(userService.getUsersByDeptId(deptId));
    }


}
